
package org.jeecgframework.core.util.icp;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.StringUtil;

/**
 * 
 * <p>
 * 标题: 根据抓取网页来获取ICP信息
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since Mar 26, 2014 3:26:29 PM
 */
public class IcpUtil {
	final static String ICP_URL = "http://ip.chinaz.com/?IP=";
	
	final static String SEO_URL = "http://seo.chinaz.com/?host=";
	
	//<strong class="red">查询结果[1]: 115.239.210.27 ==&gt;&gt; 1945096731 ==&gt;&gt; 浙江省杭州市 北京百度网讯科技有限公司电信节点</strong>
	
	public static Map<String , String> getIcpInfo(String url){
		Map<String , String> icpInfoMap = new HashMap<String , String>();;
		try{
			
			
			HtmlCleaner cleaner = new HtmlCleaner();
			CleanerProperties props = cleaner.getProperties();
			props.setUseCdataForScriptAndStyle(true);
			props.setRecognizeUnicodeChars(true);
			props.setUseEmptyElementTags(true);
			props.setAdvancedXmlEscape(true);
			props.setTranslateSpecialEntities(true);
			props.setBooleanAttributeValues("empty");
			TagNode node = cleaner.clean(new URL(ICP_URL+url),"utf-8");
			
			String content = new PrettyXmlSerializer(props).getAsString(node);
			
			int result = content.indexOf("<strong class=\"red\">查询结果[1]:");
			
			String strong = content.substring(result+"<strong class=\"red\">查询结果[1]:".length(), content.indexOf("</strong>",result));
			
			if(StringUtil.isNotEmpty(strong)){
				String [] info  = strong.split("==&gt;&gt;");
				if(info.length == 3){
					icpInfoMap = new HashMap<String , String>();
					icpInfoMap.put("ip", info[0].trim());
					icpInfoMap.put("address", info[2].trim());
				}
			}
			
			return icpInfoMap;
		}catch(Exception e){
			icpInfoMap.put("ip", "127.0.0.1");
			icpInfoMap.put("address", "其他");
			return icpInfoMap;
		}
	}
	
	/**
	 * 根据Seo查询获取IP和地址
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static Map<String , String> getIcpInfo4Seo2(String url) throws Exception{
		Map<String , String> icpInfoMap = new HashMap<String , String>();
//		System.out.println(SEO_URL+url);
		/**
		 * 循环3次
		 * 
		 */
		
		boolean success = false;
		int time = 0;
		while(success == false && time <=3){
			try{
				HtmlCleaner cleaner = new HtmlCleaner();
				CleanerProperties props = cleaner.getProperties();
				props.setUseCdataForScriptAndStyle(true);
				props.setRecognizeUnicodeChars(true);
				props.setUseEmptyElementTags(true);
				props.setAdvancedXmlEscape(true);
				props.setTranslateSpecialEntities(true);
				props.setBooleanAttributeValues("empty");
				TagNode node = cleaner.clean(new URL(SEO_URL+url),"utf-8");
				
				String xpath = "//tr[@class='seo_item']//td[@title='域名IP']";
				
				Object[] obj = node.evaluateXPath(xpath);
				
				TagNode ipNode = (TagNode)obj[0];
		
				String content = null;
				
				if(obj != null) content = ipNode.getParent().getChildTags()[1].getText().toString();
				
				if(StringUtil.isNotEmpty(content)){
					content = content.substring(0,content.indexOf("]"));
					
					String [] info  = content.split("\\[");
					if(info.length == 2){
						icpInfoMap.put("ip", info[0].trim());
						icpInfoMap.put("address", info[1].trim());
					}
				}
				success = true;
				return icpInfoMap;
			}catch(Exception e){
				icpInfoMap.put("ip", "127.0.0.1");
				icpInfoMap.put("address", "其他");
				time++;
				if(time == 3)
					return getIcpInfo(url);
			}
		}
		
		return icpInfoMap;
		
	}
	
	
	public static String getSiteFromUrl(String url){
		url = url.replace("http:", "");
		url = url.replaceAll("\\/\\/", "");
		int position = url.indexOf("/");
		if(position<=0) return url;
		return url.substring(0,position);		
	}
	
	public static void main(String[] args) {
		try {
			String url = "http://mt.sohu.com/20150606/n414551824.shtml";
			System.out.println(IcpUtil.getIcpInfo4Seo2(url));
			System.out.println(IcpUtil.getIcpInfo(url));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
